#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n, m;
char s[maxn], t[maxn];
vector<int> a,b;
int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  scanf("%d%d%s%s", &n,&m, s, t);
  int pos = 0;
  for(int i=0; i<m; ++i) {
    while(t[i]!=s[pos])pos++;
    a.push_back(pos);
    pos++;
  }
  pos = n-1;
  for(int i=m-1; i>=0; --i) {
    while(t[i]!=s[pos])pos--;
    b.push_back(pos);
    pos--;
  }
  int ans=0;
  reverse(b.begin(),b.end());
  for(int i=0; i<m-1; ++i) {
    ans = max(ans, b[i+1]-a[i]);
  }
  printf("%d\n", ans);
}
